package fun.yizhierha.tools.other.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BadRequestException;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.exception.InternalServerException;
import fun.yizhierha.common.utils.PageUtils;
import fun.yizhierha.common.utils.Query;
import fun.yizhierha.common.utils.ValidList;
import fun.yizhierha.common.utils.file.ExcelUtils;
import fun.yizhierha.tools.other.domain.ToolEmailConfig;
import fun.yizhierha.tools.other.domain.vo.CreateToolEmailConfigVo;
import fun.yizhierha.tools.other.domain.vo.UpdateToolEmailConfigVo;
import fun.yizhierha.tools.other.domain.vo.RetrieveToolEmailConfigVo;
import fun.yizhierha.tools.other.mapper.ToolEmailConfigMapper;
import fun.yizhierha.tools.other.service.mapstruct.ToolEmailConfigMapstruct;
import fun.yizhierha.tools.other.service.ToolEmailConfigService;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/** generated by EH-Admin
* @author 二哈
* @date Fri Jan 13 20:37:24 CST 2023
**/
@Service
@RequiredArgsConstructor
public class ToolEmailConfigServiceImpl extends ServiceImpl<ToolEmailConfigMapper, ToolEmailConfig> implements ToolEmailConfigService{

    private final ToolEmailConfigMapstruct toolEmailConfigMapstruct;

    @Override
    public PageUtils<ToolEmailConfig> list(RetrieveToolEmailConfigVo retrieveToolEmailConfigVo, Query.PageVo pageVo) {
        QueryWrapper<ToolEmailConfig> wrapper = new QueryWrapper<>();
        String fromUser = retrieveToolEmailConfigVo.getFromUser();

        if (fromUser != null){
            wrapper.like(ToolEmailConfig.COL_FROM_USER,fromUser);
        }


        IPage<ToolEmailConfig> iPage = baseMapper.selectPage(new Query<ToolEmailConfig>().getPage(pageVo), wrapper);
        return new PageUtils<>(iPage);
    }

    @Override
    public synchronized void save(CreateToolEmailConfigVo createToolEmailConfigVo) {
        // 1.字段为UNI，需要不重复
        // 2.映射数据
        ToolEmailConfig toolEmailConfig = toolEmailConfigMapstruct.toToolEmailConfig(createToolEmailConfigVo);
        // 3.保存    
        this.save(toolEmailConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void edit(ValidList<UpdateToolEmailConfigVo> updateToolEmailConfigVoList, List<BaseErrDto> errDtoList) {
        List<ToolEmailConfig> toUpdateToolEmailConfigList = new ArrayList<>();

        for (UpdateToolEmailConfigVo updateToolEmailConfigVo : updateToolEmailConfigVoList) {
            Long id = updateToolEmailConfigVo.getId();
            // 1.字段为UNI，需要不重复

            ToolEmailConfig toolEmailConfig = toolEmailConfigMapstruct.toToolEmailConfig(updateToolEmailConfigVo);

            toUpdateToolEmailConfigList.add(toolEmailConfig);
        }

        // 2.更新
        this.updateBatchById(toUpdateToolEmailConfigList);
    }

    @Override
    public void remove(Set<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void download(HttpServletResponse response) {
        ExcelUtils.export(response,"邮件配置信息表",this.list(), ToolEmailConfig.class);
    }

    @Override
    public ToolEmailConfig getActive() {
        ToolEmailConfig one = null;
        try {
            one = this.getOne(new QueryWrapper<ToolEmailConfig>().eq(ToolEmailConfig.COL_ACTIVE, true));
        }catch (Exception e){
            throw new InternalServerException("激活的邮件配置不止一个，请核查业务逻辑");
        }

        return one;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void active(Long configId) {
        if (configId == null) throw new BadRequestException("configId不能为空");
        // 将其他激活配置改为未激活
        this.update(new UpdateWrapper<ToolEmailConfig>()
                .eq(ToolEmailConfig.COL_ACTIVE,true)
                .set(ToolEmailConfig.COL_ACTIVE,false));
        // 激活当前配置
        this.update(new UpdateWrapper<ToolEmailConfig>()
                .eq(ToolEmailConfig.COL_CONFIG_ID,configId)
                .set(ToolEmailConfig.COL_ACTIVE,true)
        );
    }

}